package com.module.aichat.ui.chatMain

import android.app.Dialog
import android.content.Context
import android.os.Bundle
import android.text.Editable
import android.text.Selection
import android.text.TextWatcher
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.WindowManager
import com.common.base.app.extras.*
import com.common.base.utils.AppUtils
import com.common.base.utils.ToastUtils
import com.common.base.utils.ViewUtils
import com.module.aichat.R
import com.module.aichat.databinding.AichatDialogChatInputBinding

/**
 *
 * 作者：sosou
 *
 * 版本：1.0
 *
 * 创建日期：6/3/2023
 *
 * 描述：鸡蛋兑换弹框
 *
 * 修订历史：
 *
 */
class ChatInputDialog(context: Context) :
    Dialog(context, R.style.AiChatPopupDialog), View.OnClickListener {

    private var binding: AichatDialogChatInputBinding? = null

    /**
     *
     */
    private val mContext: Context

    /**
     * 点击监听
     */
    private var listener: OnSendMsgClickListener? = null

    /**
     * 最大字符限制
     */
    val maxNum = 3000

    /**
     * +
     * 构造方法
     *
     * @param context
     */
    init {
        mContext = context
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = AichatDialogChatInputBinding.inflate(LayoutInflater.from(context))
        binding?.root?.let { setContentView(it) }

        binding?.sendMsgBtn?.setOnClickListener(onClick = this)

        binding?.sendMsgEdt?.let { edtIt->
            edtIt.addTextChangedListener(object : TextWatcher {
                override fun beforeTextChanged(charSequence: CharSequence, i: Int, i1: Int, i2: Int) {}
                override fun onTextChanged(charSequence: CharSequence, i: Int, i1: Int, i2: Int) {
                    var editable = edtIt.text
                    val len = editable?.length?:0
                    if (len > maxNum) {
                        var selEndIndex: Int = Selection.getSelectionEnd(editable)
                        val str = editable.toString()

                        //截取新字符串
                        val newStr = str.substring(0, maxNum)
                        edtIt.setText(newStr)
                        editable = edtIt.text

                        //新字符串的长度
                        val newLen = editable?.length?:0
                        //旧光标位置超过字符串长度
                        if (selEndIndex > newLen) {
                            selEndIndex = editable?.length?:0
                        }
                        //设置新光标所在的位置
                        Selection.setSelection(editable, selEndIndex)
                        ToastUtils.showToast("最多输入$maxNum" + "" + "个字")
                    }
                }

                override fun afterTextChanged(editable: Editable) {

                    //如果改变了，并且文本长度>0
                    if (editable.toString().trim { it <= ' ' }.isNotEmpty()) {
                        binding?.sendMsgBtn?.setTextColor(AppUtils.getColor(R.color.product_color))
                    } else {
                        binding?.sendMsgBtn?.setTextColor(AppUtils.getColor(R.color.white))
                    }
                }
            })
        }

    }

    override fun onClick(view: View) {

        // 防止多次点击
        if (ViewUtils.isFastClick()) return

        binding?.apply {
            when (view) {
                sendMsgBtn -> {
                    var myMsg: String = binding?.sendMsgEdt?.text.toString()
                    if (myMsg.length >= 31) {
                        myMsg = myMsg.substring(0, myMsg.length - 1)
                    }
                    myMsg = myMsg.trim { it <= ' ' }
                    myMsg.isNullOrEmpty().no {
                        listener?.onSendMsgClick(myMsg)
                    }.otherwise {
                        ToastUtils.showToast("内容不能为空！")
                    }

                    binding?.sendMsgEdt?.setText("")
                }
            }
        }

    }

    override fun show() {
        //获取当前Activity所在的窗体
        val dialogWindow = window
        //设置Dialog从窗体底部弹出
        dialogWindow?.setGravity(Gravity.BOTTOM)
//        dialogWindow?.decorView?.setPadding(55, 0, 55, 0)
        val lp = dialogWindow?.attributes
        //设置宽
        lp?.width = WindowManager.LayoutParams.MATCH_PARENT
        //设置高
//        lp?.height = WindowManager.LayoutParams.WRAP_CONTENT
        dialogWindow?.attributes = lp
//        //显示对话框
//        dialogWindow?.setGravity(Gravity.CENTER)
        //设置dialog的动画效果
        dialogWindow?.setWindowAnimations(com.common.app.R.style.bottomShowStyle)
        super.show()
    }

    override fun dismiss() {
            listener?.onDismissClick()
        super.dismiss()
    }


    /**
     * 外部调用接口
     */
    interface OnSendMsgClickListener {
        fun onSendMsgClick(msg: String?)
        fun onDismissClick()
    }
}